import React from "react"
import * as apis from "@/apis"
import { useLoading } from "@/tools/hooks";
import { FormInstance } from "@/components/base";

/** 订单详情 */
export function useOrderDetails(id?: React.Key | null, form?: FormInstance) {

    const { run, loading } = useLoading();
    const [orderDetails, setOrderDetails] = React.useState<any>(null);

    // 从服务器获取数据
    React.useEffect(() => {
        if (!id) return;
        run(async () => {
            const res = await apis.order.details.get(id)
            setOrderDetails(res);
        })
    }, [id, run])

    // 数据回填到表单
    React.useEffect(() => {
        if (!orderDetails || !form) return;
        form.resetFields();
        form.setFieldsValue(orderDetails);
    }, [orderDetails, form])

    return {
        loading,
        orderDetails,
    }
} 